import { Service } from "cl-admin";
import BaseService from "@/service/base";
import { isArray, last } from "cl-admin/utils";
@Service("cart")
class Cart extends BaseService {
	// 全部列表
	list() {
		return Promise.resolve([
			/* 数据列表 */
		]);
	}
	cartStatus(val) {
		switch (val) {
			case 1:
				val = "生效中";
				break;
			case 2:
				val = "已开单";
				break;
			case 3:
				val = "已确认";
				break;
			case 4:
				val = "已流失";
				break;
			case 5:
				val = "已超时";
				break;
			case 6:
				val = "流失中";
				break;
			case 7:
				val = "已流失";
				break;
			case 99:
				val = "删除";
				break;
		}
		return val;
	}
	// 分页列表
	page({ appoint_date, size, is_undetermined, appoint_dept_id, bra_id = [], ...args }) {
		let params = {
			search_type: 0,
			pagesize: size,
			is_undetermined,
			bra_id: bra_id.join(),
			...args
		};
		if (appoint_dept_id && isArray(appoint_dept_id)) {
			params.appoint_dept_id = last(appoint_dept_id);
		}
		if (appoint_date && appoint_date.length && is_undetermined == 2) {
			params.btime = appoint_date[0];
			params.etime = appoint_date[1];
		}

		return this.$post("/lists", params).then(res => {
			return {
				list: (res.data ?? []).map(v => ({
					...v,
					status_text: this.cartStatus(v.status),
					total_price: v.service_info.reduce(
						(prev, curr) => prev + curr.unit_price * 1,
						0
					)
				})),
				pagination: {
					...params,
					size,
					total: res.total
				}
			};
		});
	}
	// 新增
	add(params) {
		return this.$post("/add", params);
	}
	// 更新，包含 id
	update(params) {
		return this.$post("/edit", params);
	}
	// 详情，传行 id
	info({ id }) {
		return Promise.resolve({
			/* 用户信息 */
		});
	}
	// 删除，ids = [1, 2, 3]
	remove({ ids }) {
		return Promise.resolve();
	}
	// 预约跟进- 添加
	addFollow(params) {
		return this.$post("/addFollow", params);
	}

	//流失
	runoff(params) {
		return this.$post("/del", params);
	}
	//已处理 （已流失) 申诉功能
	appeal(params) {
		return this.$post("/appeal", params);
	}

	//预约 - 获取默认服务
	getDefaultser(params) {
		return this.$get("/getdefaultitem", { params });
	}

	//维护
	maintain(params) {
		return this.$post('/maintain', params)
	}

	//获取历史服务
	getService(params) {
		return this.$get('/getService', { params })
	}
	// 预约看板
	cartBoard(params) {
		return this.$get('/cartBoard', { params })
	}
	/** *
	 * 更新预约（部分更新）
	 * @parmas update_type 1针对项目等，2针对预约时间，3针对修改预约部门、预约人
	 */
	updateCart(params) {
		if (params.add_dept_id && isArray(params.add_dept_id)) {
			params.add_dept_id = last(params.add_dept_id);
		}
		if (params.order_date_id_dept && isArray(params.order_date_id_dept)) {
			params.order_date_id_dept = last(params.order_date_id_dept);
		}
		return this.$post('/updateCart', params)
	}

	//改约记录
	amendRecode(params) {
		return this.$get('/appointLog', { params })
	}

	arrived(params) {
		return this.$post('/arrived', params)
	}

}
export default Cart;
